package cn.kgc.javaweb.dao.impl;

import cn.kgc.javaweb.dao.RoleDao;
import cn.kgc.javaweb.entity.Role;
import cn.kgc.javaweb.utils.DataSourceUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @Author: 课工场
 * @Version: v1.0  2023/5/23
 * @Description:
 */
public class RoleDaoImpl implements RoleDao {

    @Override
    public List<Role> getList() throws SQLException {

        QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSource());
        String sql = "select id ,role_name as roleName  from role";
        List<Role> roles = queryRunner.query(sql, new BeanListHandler<>(Role.class));

        return roles;
    }

    @Override
    public List<Role> getPage(Integer start, int limte) {

        QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSource());
        String sql = "select id ,role_name as roleName ,create_time as createTime , update_time as updateTime from role limit ?,?";
        try {
            List<Role> roles = queryRunner.query(sql, new BeanListHandler<>(Role.class), start, limte);
            return  roles;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public Long getTotal() {

        QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSource());
        String sql = "select count(*) from role";
        try {
            Long count = queryRunner.query(sql, new ScalarHandler<>());
            return count;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public void insertRole(String roleName) throws SQLException {

        QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSource());
        String sql = "insert into role (role_name) values(?)";
        queryRunner.execute(sql,roleName);

    }

    @Override
    public Role getById(Integer id) {

        QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSource());
        String sql = "select id ,role_name as roleName from role where id = ?";
        try {
            Role role = queryRunner.query(sql, new BeanHandler<>(Role.class), id);
            return role;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public void updateById(String roleName, Integer id) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSource());
        String sql = "update role set role_name = ? where id = ?";
        queryRunner.execute(sql,roleName,id);
    }

    @Override
    public void deleteById(Integer id) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSource());
        String sql = "delete from role where id = ?";
        queryRunner.execute(sql,id);
    }
}
